import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_acrylic/flutter_acrylic.dart';
import 'package:flutter_acrylic/window_effect.dart';
import 'package:win32_registry/win32_registry.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Window.initialize();
  runApp(const MyApp());
  _applyAcrylicV1();
}

Future<void> _applyAcrylicV1() async {
  Color color =
      Platform.isWindows ? const Color(0xb3ffffff) : Colors.transparent;
  await Window.setEffect(
      effect: WindowEffect.acrylic, color: color, dark: false);
}

Future<void> _applyAcrylicV2() async {
  const regCurrentVersionKey = r'SOFTWARE\Microsoft\Windows NT\CurrentVersion';
  final key =
      Registry.openPath(RegistryHive.localMachine, path: regCurrentVersionKey);

  final buildNumber = key.getValueAsInt('UBR');
  WindowEffect effect;
  if (buildNumber! >= 1803) {
    effect = WindowEffect.acrylic;
  } else {
    effect = WindowEffect.aero;
  }
  print("effect:$effect");
  Color color =
      Platform.isWindows ? const Color(0xb3ffffff) : Colors.transparent;
  await Window.setEffect(effect: effect, color: color, dark: false);
  key.close();
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    // _applyAcrylic();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              '',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: const FloatingActionButton(
        onPressed: _applyAcrylicV1,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
